package com.gimi.cloud.bbp.client.service.product.impl;

import com.gimi.cloud.bbp.client.service.product.ProductService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelDao;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelValueDao;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCategoryDao;
import com.gimi.cloud.bbp.dao.dao.product.*;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.product.ProductDto;
import com.gimi.cloud.bbp.dto.product.ProductSkuDto;
import com.gimi.cloud.bbp.entity.prod.EcProdCatModelEntity;
import com.gimi.cloud.bbp.entity.product.ProductEntity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class ProductServiceImpl implements ProductService {
	private static Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ProductImgDao productImgDao;
	@Autowired
	private ProductSkuDao productSkuDao;
	@Autowired
	private EcProdCategoryDao prodCategoryDao;
	@Autowired
	private EcProdCatModelDao prodCatModelDao;
	@Autowired
	private EcProdCatModelValueDao prodCatModelValueDao;
	@Autowired
	private ProductSkuImgDao productSkuImgDao;
	@Autowired
	private ProductValueAddDao productValueAddDao;
	@Autowired
	private EcProdCatModelValueDao modelValueDao;

	/**
	 * 查询商品详情信息
	 */
	@Override
	public ProductDto getProductDetail(ProductDto productDto) throws Exception {
		String prodId = productDto.getProdId();
		if (prodId != null) {
			ProductDto dto = productDao.findByProdId(prodId);
			//商品图片
			List<String> list = productImgDao.selectUrlByprodId(prodId);
				if (list != null && list.size()>0) {
					dto.setProductImgUrl(list);
				}else {
					dto.setProductImgUrl(new ArrayList<>());
				}
			
			return dto;
		} else {
			throw new MsgException(ResponseCodeEnum.FAILURE, "商品id不能为空!");
		}
	}

	/**
	 * 商品规格属性列表
	 */
	@Override
	public List<Map<String, Object>> getProductProp(ProductDto productDto) throws Exception {
		ProductEntity entity = productDao.selectByPrimaryKey(productDto.getProdId());
		// 颜色/内存
		List<EcProdCatModelEntity> list = prodCatModelDao.findCatModelByCatId(entity.getProdCatId());
		List<Map<String, Object>> propList = new ArrayList<>();

		for (EcProdCatModelEntity prodCatModel : list) {
			Map<String, Object> map = new HashMap<>();
			List<String> modleValueList = prodCatModelValueDao.selectModelValueByModelId(prodCatModel.getModelId());
			//map.put(prodCatModel.getProdPropName(), modleValueList);
			map.put("model", prodCatModel.getProdPropName());
			map.put("modelValue", modleValueList);
			propList.add(map);
		}
		return propList;
	}

	/**
	 * 商品有sku库存的属性列表
	 */
	@Override
	public List<ProductSkuDto> getModelValueList(ProductDto productDto) throws Exception {
		//这里返回全部sku列表,app根据库存数量来判断是否可选
		List<ProductSkuDto> skuDtoList = productSkuDao.selectByProdIdAndStock(productDto.getProdId());// (25,26),(25,21)

		for (ProductSkuDto productSkuDto : skuDtoList) {
			List<Map<String, Object>> modelValueList = prodCatModelValueDao
					.getModelIdByIds(productSkuDto.getSkuValue());
			// 查询每条有库存的sku对应的规格
			if (modelValueList != null && modelValueList.size() > 0) {
				List<Map<String, Object>> list2 = new ArrayList<>();
				for (Map<String, Object> map : modelValueList) {
					Map<String, Object> mapValue = new HashMap<>();
					//mapValue.put(map.get("skuDetailKey"), map.get("skuDetailValue"));// [{"规格":"1g"},{"颜色":"琥珀B02"}]
					mapValue.put("model", map.get("skuDetailKey"));
					mapValue.put("modelValue", map.get("skuDetailValue"));
					list2.add(mapValue);
				}
				productSkuDto.setSkuValueDetail(list2);
			}
			// 单品图片
			List<String> skuImgList = productSkuImgDao.getSkuImgsByProdId(productSkuDto.getSkuId());
			if (skuImgList != null && skuImgList.size() > 0) {
				productSkuDto.setProductSkuImgs(skuImgList);
			} else {
				productSkuDto.setProductSkuImgs(new ArrayList<>());
			}
		}
		logger.info("该商品有库存的sku组合列表为==>" + JsonUtil.toJson(skuDtoList));
		return skuDtoList;
	}

	@Override
	public List<ProductDto> getProductsOfThirdGrade(List<String> thirdCategoriesId) {
		return productDao.getProductsByThirdGradeCategoryId(thirdCategoriesId);
	}

	@Override
	public List<String> getChildGradeCategories(String parentCatId, Integer selectCategoryGrade) {
		Map<String, Object> param = new HashMap<>();
		param.put("parentCatId", parentCatId);
		String sql = "select t.prod_cat_id from ec_prod_category t where t.parent_cat_id=:parentCatId and is_delete=0";
		if(selectCategoryGrade != null){
			param.put("currentGrade", selectCategoryGrade);
			sql += " and cat_grade=:currentGrade";
		}
		QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParams(param);
		return prodCategoryDao.getChildCategoriesIdByParentIdAndGrade(queryParam);
	}

	@Override
	public List<String> getThirdGradeCategoriesByFirstGradeCategoryId(String firstCatId) {
		return prodCategoryDao.getThirdGradeCategoriesByFirstGradeCategoryId(firstCatId);
	}

	@Override
	public List<ProductDto> searchProducts(ProductDto productDto) {
		return productDao.searchProducts(productDto);
	}

	@Override
	public ProductDto findProductDetail(ProductDto productDto) throws Exception {
		// 验证是否是当前店铺,商品状态
		String prodId = productDto.getProdId();
		ProductDto dto = productDao.findOneNormalByProductId(prodId);
		if (dto == null || StringUtil.isBlank(dto.getProdId())) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_EXCEPTION, "未获取到商品信息!");
		} else if (!new Integer(1).equals(dto.getProdStatus())) {
			throw new MsgException(ResponseCodeEnum.PROD_STATUS_ERR, "商品已下架!");
		}
		dto.setProdStatus(null);
		try {
			dto.setSpecParameter(new String(dto.getSpecParameters(), "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
			throw new MsgException(ResponseCodeEnum.PROD_STATUS_ERR, "商品规格参数异常!");
		}
		dto.setSpecParameters(null);
		dto.setProductValueAddEntity(productValueAddDao.selectByPrimaryKey(prodId));
		dto.setProductImgs(productImgDao.getImgsByProdId(prodId));
		List<ProductSkuDto> skuDto = productSkuDao.selectByProdId(prodId);
		if (skuDto != null && skuDto.size() > 0) {
			for (ProductSkuDto skus : skuDto) {
				List<String> list = productSkuImgDao.getSkuImgsByProdId(skus.getSkuId());
				if (list != null && list.size() > 0) {
					skus.setProductSkuImgs(list);
				} else {
					skus.setProductSkuImgs(new ArrayList<>());
				}
				if (StringUtil.isNotBlank(skus.getSkuValue())) {
					skus.setSkuValueDetail(modelValueDao.getModelIdByIds(skus.getSkuValue()));
					skus.setSkuValue(null);
				}
				skus.setSkuId(null);
			}
		}
		dto.setProductSkuDto(skuDto);
		return dto;
	}
}
